<template>
  <div>
    <el-card style="margin: 20px 0">
      <category-select @getCategoryId="getCategoryId" :disableSelect="!isShowTable"></category-select>
    </el-card>
    <el-card>
      <div v-show="isShowTable">
        <el-button type="primary" icon="el-icon-plus" @click="addAttr" :disabled="!category3Id">添加属性</el-button>
        <el-table style="width: 100%" border :data="attrList">
          <el-table-column type="index" label="序号" width="80" align="center"> </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="150"> </el-table-column>
          <el-table-column prop="prop" label="属性值列表" width="width">
            <template slot-scope="{ row }">
              <el-tag type="success" v-for="item in row.attrValueList" :key="item.id" style="margin: 0 20px">{{ item.valueName }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="150">
            <template slot-scope="{ row, $index }">
              <el-button type="warning" icon="el-icon-edit" size="mini" @click="updateAttr(row)"></el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div v-show="!isShowTable">
        <el-form :inline="true" ref="form" label-width="80px" :model="attrInfo">
          <el-form-item label="属性">
            <el-input v-model="attrInfo.attrName" placeholder="请输入属性名"></el-input>
          </el-form-item>
        </el-form>

        <el-button type="primary" icon="el-icon-plus" @click="addAttrValue" :disabled="!attrInfo.attrName">添加属性值</el-button>
        <el-button @click="isShowTable = true">取消</el-button>

        <el-table :data="attrInfo.attrValueList" style="width: 100%; margin: 20px 0" border>
          <el-table-column type="index" label="序号" width="80" align="center"> </el-table-column>
          <el-table-column label="属性值名称" width="width">
            <template slot-scope="{row, $index}">
              <el-input v-model="row.valueName" placeholder="请输入属性值" size="mini" v-if="row.flag" @blur="toLook(row)" @keyup.native.enter="toLook(row)" :ref="$index"></el-input>
              <span v-else @click="toEdit(row, $index)" style="display: block">{{row.valueName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{row, $index}">
              <el-popconfirm :title="`确定删除${row.valueName}？`" @onConfirm="deleteAttrValue($index)">
                <el-button type="danger" size="mini" icon="el-icon-delete" slot="reference"></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-button type="primary" @click="addOrUpdateAttr" :disabled="attrInfo.attrValueList.length<1">保存</el-button>
        <el-button @click="isShowTable = true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import cloneDeep from 'lodash/cloneDeep'
export default {
  name: 'Attr',
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      attrList: [],
      isShowTable: true,
      attrInfo: {
          attrName: "",
          attrValueList: [],
          categoryId: 0,
          categoryLevel: 0,
          id: 0
      },
      flag: true
    }
  },
  methods: {
    getCategoryId({ categoryID, level }) {
      if (level == 1) {
        this.category1Id = categoryID
        this.category2Id = ''
        this.category3Id = ''
      } else if (level == 2) {
        this.category2Id = categoryID
        this.category3Id = ''
      } else {
        this.category3Id = categoryID
        this.getAttrList()
      }
    },
    // 获取平台属性列表数据
    async getAttrList() {
      const { category1Id, category2Id, category3Id } = this
      let result = await this.$API.attr.reqAttrList(category1Id, category2Id, category3Id)
      this.attrList = result.data
    },
    // 添加平台属性值
    addAttrValue() {
      this.attrInfo.attrValueList.push({
        arrtId: this.attrInfo.id,
        valueName: '',
        // 显示input输入框
        flag: true
      })
      this.$nextTick(() => {
        this.$refs[this.attrInfo.attrValueList.length-1].focus()
      })
    },
    // 添加平台属性
    addAttr() {
      this.isShowTable = false
      this.attrInfo = {
        attrName: "",
        attrValueList: [],
        categoryId: this.category3Id,
        categoryLevel: 0,
        id: 0
      }
    },
    // 更新/修改平台属性
    updateAttr(row) {
      this.isShowTable = false
      this.attrInfo = cloneDeep(row)
      this.attrInfo.attrValueList.forEach(item => {
        this.$set(item, 'flag', false)
      })
    },
    // 查看模式（显示span）
    toLook(row){
      if(row.valueName.trim() == '') {
        this.$message('请输入正确的属性值')
        return
      }
     let isRepeat =  this.attrInfo.attrValueList.some(item => {
      // 只和原有数据做比较
        if(row!=item) {
          return row.valueName.trim() == item.valueName
        }
      })
      if(isRepeat) {
        this.$message('属性值不能重复')
        return
      }
      row.flag = false
    },
    // 编辑模式（显示input输入框）
    toEdit(row, index){
      row.flag = true
      this.$nextTick(() => {
        this.$refs[index].focus()
      })
    },
    // 删除平台属性
    deleteAttrValue(index) {
      this.attrInfo.attrValueList.splice(index, 1)
    },
    // 保存按钮的回调
    async addOrUpdateAttr() {
      this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter(item => {
        if(item.valueName!='') {
          delete item.flag
          return true
        }
      })
      try {
        await this.$API.attr.reqAddOrUpdateAttr(this.attrInfo)
        this.isShowTable = true
        this.$message({type: 'success', message: '保存成功'})
        this.getAttrList()
      } catch (error) {
      }
    }
  }
}
</script>

<style scoped></style>
